package com.oreilly.android.taskmanager;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

public class TaskManagerLocationManager implements LocationListener {

	private static final long LOCATION_UPDATE_TIME = 1000;
	private static final float LOCATION_UPDATE_DISTANCE = 10;
	private LocationManager locationManager;
	private Location latestLocation;

	private boolean mockEnabled = true;

	public TaskManagerLocationManager(Context context) {
		locationManager = (LocationManager) context
				.getSystemService(Context.LOCATION_SERVICE);

		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
				LOCATION_UPDATE_TIME, LOCATION_UPDATE_DISTANCE, this);

		setLatestLocation(locationManager
				.getLastKnownLocation(LocationManager.NETWORK_PROVIDER));

		setMockLocation();
	}

	public void onLocationChanged(Location location) {
		setLatestLocation(location);
	}

	public void onProviderDisabled(String provider) {

	}

	public void onProviderEnabled(String provider) {

	}

	public void onStatusChanged(String provider, int status, Bundle extras) {

	}

	public Location getLatestLocation() {
		return latestLocation;
	}

	public void setLatestLocation(Location latestLocation) {
		this.latestLocation = latestLocation;
	}

	private void setMockLocation() {
		if (mockEnabled) {
			Location location = new Location(LocationManager.GPS_PROVIDER);
			location.setLatitude(43.220658);
			location.setLatitude(-75.408508);

			locationManager.addTestProvider(LocationManager.GPS_PROVIDER, true,
					true, false, false, false, false, false, 10, 10);

			locationManager.setTestProviderLocation(
					LocationManager.GPS_PROVIDER, location);

			locationManager.setTestProviderEnabled(
					LocationManager.GPS_PROVIDER, true);
		}
	}
}
